package aden.SifWar;

import aden.Generic.GenMain;
import aden.HexEnv.HexEnvTile;
import aden.HexEnv.HexEnvWorld.HexEnvIterTile;

class SifWarPlayer
{
	
	public static final int Status_HOTSEAT = 0;
	public static final int Status_ONLINE = 1;
	public static final int Status_Undefined = 2;
	private int mStatus;
	public int GetStatus() {return mStatus;}

	public static final int Type_HUMAN = 0;
	public static final int Type_IA = 1;
	public static final int Type_Undefined = 2;
	private int mType;
	public int GetType() {return mType;}
	
	private SifWarPlayerMgr mMgr;
	public SifWarPlayer(SifWarPlayerMgr inMgr)
	{
		mMgr = inMgr;
		
		mHeight = mMgr.GetMain().GetSifWarWorld().GetHeight();
		mWidth = mMgr.GetMain().GetSifWarWorld().GetWidth();
		mBoard = new PlayerTile[mWidth][mHeight];
		
		for (int i = 0;
			 i < mWidth;
				++i)
		{
			for (int j = 0;
					 j < mHeight;
						++j)
				{
				mBoard[i][j] = new PlayerTile();
				}			
		}
	}
	
	public SifWarMain GetSifWarMain() {return (SifWarMain)mMgr.GetMain();}
	
	public SifWarPlayerMgr GetPlayerMgr() {return mMgr;}

	
	private SifWarUnit mFirstUnit;
	public void AddUnit(SifWarUnit inUnit,int inX,int inY)
	{
		inUnit.mNext = mFirstUnit;
		if (mFirstUnit != null)
		{
			mFirstUnit.mPrev = inUnit;
		}
		mFirstUnit = inUnit;
		mBoard[inX][inY].mUnit = inUnit;
	}
	
	public void RemoveUnit(SifWarUnit inUnit)
	{
		mBoard[inUnit.GetX()][inUnit.GetY()].SetUnit(null);
		
		// TODO il faut enlever l'unite ennemie des autres boards
		for (int i = 0;
				i < mMgr.GetPlayerCount();
				++i)
		{
			mMgr.GetPlayer(i).GetTile(inUnit.GetX(),inUnit.GetY()).SetEnnemyUnit(null);
			// TODO il faut laisser des traces
		}

		
		if (inUnit != mFirstUnit)
		{
			inUnit.mPrev.mNext = inUnit.mNext;
		}
		else
		{
			mFirstUnit = inUnit.mNext;
		}
		
		if (inUnit.mNext != null)
		{
			inUnit.mNext.mPrev = null;
		}
	}
	
	
    private PlayerTile mBoard[][];  
	private int mHeight;
	private int mWidth;
    
    public SifWarUnit GetFirstUnit() {return mFirstUnit;}
    
	public PlayerTile GetTile(int inX,int inY)
	{
		return mBoard[inX][inY];
	}
	
	public boolean MoveEnnemyUnit(int        inOldX, 
			                      int        inOldY,
			                      SifWarUnit inUnit)
	{
		mBoard[inOldX][inOldY].mEnnemyUnit = null;
		++(mBoard[inOldX][inOldY].mEnnemyUnitViewHitCount);
		mBoard[inOldX][inOldY].mEnnemyUnitViewHitAge = 0;
		mBoard[inOldX][inOldY].mEnnemyUnitViewHitUnitType = inUnit.GetUnitType();
		
		mBoard[inUnit.GetX()][inUnit.GetY()].mEnnemyUnit = inUnit;
		mBoard[inOldX][inOldY].mEnnemyUnitViewHitCount = 0;
		return true;
	}
	
	public boolean Evolve()
	{
		// TODO on fait vieillir les traces
		
		UpdateVisibility();
		return true;
	}
	
    public class PlayerTile
    {
    	private SifWarUnit mUnit;
    	public SifWarUnit GetUnit() {return mUnit;}
    	public boolean SetUnit(SifWarUnit inUnit)
    	{
    		mUnit = inUnit;
            return true;
    	}
     	    	
    	public static final int Visibility_FULL = 0;
    	public static final int Visibility_CLOSE = 1;
    	public static final int Visibility_BLURRED = 2;
    	public static final int Visibility_FAR = 3;
    	public static final int Visibility_NO = 4;
    	public static final int Visibility_Undefined = 5;
    	
    	private int mVisibility;
    	public int GetVisibility() {return mVisibility;}
    	public boolean SetVisibility(int inVisibility)
    	{
    		mVisibility = inVisibility;
            return true;
    	}
     	
    	// historique
    	int mEnnemyUnitRadioHit;
    	int mEnnemyUnitRadioHitAge;

    	int mEnnemyUnitViewHitUnitType;
    	int mEnnemyUnitViewHitAge;
    	int mEnnemyUnitViewHitCount;

    	private SifWarUnit mEnnemyUnit;
    	public SifWarUnit GetEnnemyUnit() {return mEnnemyUnit;}
    	public void SetEnnemyUnit(SifWarUnit inUnit) {mEnnemyUnit = inUnit;}
    	
    	public PlayerTile()
    	{
    		mUnit = null;
    		mVisibility = Visibility_NO;
    		
    		mEnnemyUnitRadioHit = 0;
    		mEnnemyUnitViewHitCount = 0;
    		mEnnemyUnit = null;
    	}
    	
   	
    	
    	public boolean HasDrawing()
    	{
    		return true; // mUnit != null || (mVisibility != Visibility_NO) || (mLastRadioHit != -1) || (mLastOppositeUnit != - 1);
    	}
    }
	
    public boolean UpdateVisibility()
    {
    	SifWarWorld theWorld = mMgr.GetMain().GetSifWarWorld();

    	// on initialise tout a non visible
    	for (int i = 0;
    			i < mWidth;
    			++i)
    	{
    		for (int j = 0;
    				j < mHeight;
    				++j)
    		{
    			mBoard[i][j].mVisibility = PlayerTile.Visibility_NO;
    		}			
    	}


    	// on itere sur chaque unite 
    	
    	// TODO le traitement pour chaque unite silencieuse ?!?
		
		for (SifWarUnit theUnit = mFirstUnit;
		     theUnit != null;
				theUnit = theUnit.GetNext())
		{
			theUnit.UpdateVisibility();
		}
    	
    	return true;
    }
    
}
